package com.ctgu.ImageRecognition.util;

import com.ctgu.ImageRecognition.vo.Data;
import com.ctgu.ImageRecognition.vo.Labels;
import com.google.gson.Gson;

import java.util.List;

public class GsonUtils {
    public static Labels fromJson(String json) {
        final Gson gson = new Gson();
        final Data data = gson.fromJson(json, Data.class);
        //从对象中获取集合
        if (data.getLabels().isEmpty()) {
            Labels Labels= new Labels();
            Labels.setName("其他");
            Labels.setConfidence("0");
            Labels.setFirstCategory("其他");
            Labels.setSecondCategory("其他");
            return Labels;
        }
        final List<Labels> labels = data.getLabels();
        Labels Labels= new Labels();
        Labels.setConfidence("0");
        for (Labels label : labels) {
            System.out.println("转化结果:"+label.getName() + "\t" + label.getConfidence() +
                    "\t" + label.getFirstCategory() + "\t" + label.getSecondCategory());
            //找出Confidence最大的一个label
            if (label.getConfidence().compareTo(Labels.getConfidence()) > 0) {
                Labels = label;
            }

        }
        return Labels;
    }
}
